package com.wzcl.app.controller;

import com.wzcl.app.dao.model.Requisition;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.RequisitionDto;
import com.wzcl.app.model.vo.RequisitionVo;
import com.wzcl.app.service.RequisitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/requisition")
@Api(value = "申请课程controller", tags = {"申请课程相关接口"})
public class RequisitionController {

    @Resource
    private RequisitionService service;

    @ApiOperation("添加申请")
    @PostMapping("/add")
    public RVO<RequisitionVo> addRequisition(@RequestBody @Valid RequisitionDto requisitionDto){
        requisitionDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        Requisition requisition = service.addRequisition(requisitionDto);
        return RVO.success(RequisitionVo.create(requisition));
    }

    @ApiOperation("完结申请")
    @PostMapping("/end")
    public RVO<RequisitionVo> endRequisition(@RequestBody RequisitionDto requisitionDto){
        Requisition requisition = service.endRequisition(requisitionDto);
        return RVO.success(RequisitionVo.create(requisition));
    }

    @ApiOperation("标记申请")
    @PostMapping("/star")
    public RVO<RequisitionVo> starRequisition(@RequestBody RequisitionDto requisitionDto){
        Requisition requisition = service.starRequisitionById(requisitionDto.getId());
        return RVO.success(RequisitionVo.create(requisition));
    }

    @ApiOperation("查找申请List")
    @PostMapping("/getList")
    public RVO<List<RequisitionVo>> getRequisitionList(@RequestBody RequisitionDto requisitionDto){
        List<Requisition> list = service.getRequisitionList(requisitionDto);
        if(list != null && list.size() >0){
            List<RequisitionVo> collect = list.stream().map(RequisitionVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("查找申请Page")
    @PostMapping("/getPage")
    public RVO getRequisitionPage(@RequestBody RequisitionDto requisitionDto){
        MyPage<Requisition> page = service.getRequisitionPage(requisitionDto);
        MyPage<RequisitionVo> myPage = new MyPage<>();
        if(page != null && page.getContent().size() >0){
            List<RequisitionVo> collect = page.getContent().stream().map(RequisitionVo::create).collect(Collectors.toList());
            myPage.setTotalElements(page.getTotalElements());
            myPage.setContent(collect);
            return RVO.success(myPage);
        }
        return RVO.success(null);
    }
}
